package com.lightboat.common.utils;

import com.lightboat.common.core.text.Convert;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpUtils {


    /**
     * 提取文本中的数字
     * @param text 文本
     * @param unit 单位
     * @return
     */
    public static Long extractBoxNumber(String text,String unit) {
        Pattern pattern = Pattern.compile("(\\d+)" + unit);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()){
            return Convert.toLong(matcher.group(1));
        }
        return null;
    }


    public static Long extractBoxNumber(String text,String[] units) {
        for (String unit : units) {
            Pattern pattern = Pattern.compile("(\\d+)" + unit);
            Matcher matcher = pattern.matcher(text);
            if (matcher.find()) {
                return Convert.toLong(matcher.group(1));
            }
        }
        return null;
    }

    public static Long extractNumber(String text) {
        Pattern pattern = Pattern.compile("(\\d+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return Convert.toLong(matcher.group(1));
        }
        return null;
    }

    /**
     * 提取手机号
     * @param text
     * @return
     */
    public static List<String> phoneNumberExtractor(String text) {

        // 定义正则表达式来匹配中国的手机号
        String regex = "\\b1[3-9]\\d{9}\\b";

        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);

        // 创建matcher对象
        Matcher matcher = pattern.matcher(text);

        // 存储匹配到的手机号
        List<String> phoneNumbers = new ArrayList<>();

        // 查找并添加所有匹配的手机号到列表中
        while (matcher.find()) {
            phoneNumbers.add(matcher.group());
        }

        return phoneNumbers;
    }

}
